const lodash = require('lodash')
const findDeep = require('deepdash/getFindDeep')(lodash)
const mapDeep = require('deepdash/getMapDeep')(lodash)
const treeData = require('./treeData')

const node = {
  name: 'level2',
  value: 40
}

const propMap = {
  keyProp: 'value',
  childrenProp: 'children',
  deepdashOptions: {
    childrenPath: 'children',
  }
}

function findChildren (treeData, node, propMap) {
  const searchValue = node[propMap.keyProp]
  const targetMeta = findDeep(treeData, item => {
    const itemValue = item[propMap.keyProp]
    if (itemValue === searchValue) return true
  }, propMap.deepdashOptions)

  if (!targetMeta) return []
  const { value } = targetMeta

  const children =  value[propMap.childrenProp] || []
  return mapDeep(children, v => v, propMap.deepdashOptions)
}

module.exports = findChildren

// const targetValues = findChildren(treeData, node, propMap)

// console.log(`children of node ${node.name}: \n`, targetValues)
